package com.smart.home.netty;

import com.smart.home.netty.handler.AbstractChannelInboundHandler;
import com.smart.home.util.CollectionUtils;
import com.smart.home.util.StringUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * netty工具
 *
 * @author zhangpeng
 * @version 1.0
 * @date 2017-03-09
 */
public abstract class NettyUtils {

    /**
     * 向设备发送指令
     * 推荐该方法在发送单指令并且需要最终返回信息的时候使用，多次重复调用会增大虚拟机开销
     *
     * @param channelInboundHandler 输入信息处理
     * @param host                  设备主机
     * @param port                  设备端口
     * @return AbstractChannelInboundHandler实例
     * @throws InterruptedException
     */
    public static AbstractChannelInboundHandler sendCommand(final AbstractChannelInboundHandler channelInboundHandler, String host, Integer port) {
        if (channelInboundHandler == null || StringUtils.isEmpty(host) || port == null) {
            return null;
        }
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(channelInboundHandler);
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(host, port)).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            //TODO 日志记录异常信息,方便后续调优
            return channelInboundHandler;
        } finally {
            group.shutdownGracefully();
        }
        return channelInboundHandler;
    }

    /**
     * 批量向设备发送指令
     *
     * @param channelInboundHandlerList
     * @param host
     * @param port
     * @return
     */
    public static List<AbstractChannelInboundHandler> sendCommand(List<AbstractChannelInboundHandler> channelInboundHandlerList, String host, int port) {
        if (CollectionUtils.isEmpty(channelInboundHandlerList)) {
            return null;
        }
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            for (final AbstractChannelInboundHandler handler : channelInboundHandlerList) {

                Bootstrap bootstrap = new Bootstrap();
                bootstrap.group(group)
                        .channel(NioSocketChannel.class)
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(handler);
                            }
                        });
                ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(host, port)).sync();
                channelFuture.channel().closeFuture().sync();
            }
        } catch (Exception e) {
            //TODO 日志记录异常信息,方便后续调优
            return null;
        } finally {
            group.shutdownGracefully();
        }
        return channelInboundHandlerList;
    }

}
